package com.autonavi.abs.generator.util;

import com.autonavi.abs.generator.common.Constant;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: 字符串工具类</p>
 * @date 2020-08-10 17:25
 */
public class StringUtil {

    public static String capitalize(final String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }

        char firstChar = str.charAt(0);
        if (Character.isTitleCase(firstChar)) {
            // already capitalized
            return str;
        }

        return new StringBuilder(strLen)
                .append(Character.toTitleCase(firstChar))
                .append(str.substring(1))
                .toString();
    }

    /**
     * 将下划线格式转换为第一个字母大写格式
     *
     * @param source
     * @return
     */
    public static String getFirstUpperCaseFromUnderLine(String source) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(source);
        int count = buffer.indexOf(Constant.UNDER_LINE);
        while (count != 0) {
            int num = buffer.indexOf(Constant.UNDER_LINE, count);
            count = num + 1;
            if (num != -1) {
                char ss = buffer.charAt(count);
                char ia = (char) (ss - 32);
                buffer.replace(count, count + 1, ia + Constant.BLANK_STRING);
            }
        }
        String result = buffer.toString().replaceAll(Constant.UNDER_LINE, Constant.BLANK_STRING);
        return StringUtil.capitalize(result);
    }

    /**
     * 转换为camel格式
     *
     * @param source
     * @return
     */
    public static String getCamelName(String source) {
        String upperName = getFirstUpperCaseFromUnderLine(source);
        String firstChar = upperName.substring(0, 1);
        return firstChar.toLowerCase() + upperName.substring(1);
    }

}
